const Base = require('./base.js');
const moment = require('moment');
const utils = require('../common/utils');

module.exports = class extends think.Controller {
    async notifyAction() {
        let d = this.post('xml');
        d.source = '订单支付';
        console.log('支付通知');
        const pay_add = await this.model('payment_flow').thenAdd(this.model('payment').payment_field(d), {transaction_id: d.transaction_id[0]});

        await this.model('order').where({out_trade_no: d.out_trade_no[0]}).update({
            order_status: 1,
            pay_status: 1,
            pay_price: Number(d.cash_fee[0]) / 100,
            pay_time: moment().format('YYYY-MM-DD HH:mm:ss')
        });
        const order_id = await this.model('order').where({out_trade_no: d.out_trade_no[0]}).getField('id', true);
        const goods = await this.model('order_goods').where({order_id: order_id}).getField('goods_id,number', true);
        await this.model('goods').where({id: goods.goods_id}).increment('sell_volume', goods.number);
        let success = utils.buildXML({xml: {return_code: 'SUCCESS', return_msg: 'OK'}});
        return this.json(success);
    }
};
